package com.tim.app.ui.adapter.message;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.util.TimeUtil;
import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.tim.app.R;
import com.tim.app.server.entry.OfficialNotice;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 官方通知
 */
public class OfficialNoticeAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, OfficialNotice> {

    private Context mContext;

    public OfficialNoticeAdapter(Context mContext, List<OfficialNotice> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_official_notice, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, final OfficialNotice data) {
        if (data == null) {
            return;
        }

        final ViewHolder holder = (ViewHolder) mHolder;
        holder.tvTime.setText(TimeUtil.formatTimeRuleOne(mContext, data.getCreateTime()));
        if (!TextUtils.isEmpty(data.getImage())) {
            holder.rivNoticeImage.setVisibility(View.VISIBLE);
            BitmapLoader.ins().loadImage(data.getImage(), holder.rivNoticeImage);
        } else {
            holder.rivNoticeImage.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvNoticeContent.setText(data.getContent());
        } else {
            holder.tvNoticeContent.setText("");
        }
        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvNoticeTitle.setText(data.getTitle());
        } else {
            holder.tvNoticeTitle.setText("");
        }
        if (position == getDataList().size() - 1) {
            holder.vBottomLine.setVisibility(View.VISIBLE);
        } else {
            holder.vBottomLine.setVisibility(View.GONE);
        }
        if (data.getOpenType() == 2 || data.getOpenType() == 3) {
            holder.rlBottomContainer.setVisibility(View.VISIBLE);
        } else {
            holder.rlBottomContainer.setVisibility(View.GONE);
        }
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        TextView tvTime;
        RatioImageView rivNoticeImage;
        TextView tvNoticeTitle;
        TextView tvNoticeContent;
        View vBottomLine;
        RelativeLayout rlBottomContainer;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTime = (TextView) itemView.findViewById(R.id.tvTime);
            rivNoticeImage = (RatioImageView) itemView.findViewById(R.id.rivNoticeImage);
            tvNoticeTitle = (TextView) itemView.findViewById(R.id.tvNoticeTitle);
            tvNoticeContent = (TextView) itemView.findViewById(R.id.tvNoticeContent);
            rlBottomContainer = (RelativeLayout) itemView.findViewById(R.id.rlBottomContainer);
            vBottomLine = itemView.findViewById(R.id.vBottomLine);
        }

    }

}
